home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 17135 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.3 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c++,comp.lang.c,comp.os.ms-windows.programmer.misc
  4. Subject: Re: fastest code
  5. Date: Sat, 13 Apr 96 23:22:52 GMT
  6. Organization: none
  7. Message-ID: <829437772snz@genesis.demon.co.uk>
  8. References: <316112A2.7D37@public.sta.net.cn> <4kgu7g$n9a@solutions.solon.com> <4kh2p7INNkcq@keats.ugrad.cs.ubc.ca> <4kjdus$fiq@samba.rahul.net> <4kjpn0INN817@keats.ugrad.cs.ubc.ca> <829352535snz@genesis.demon.co.uk> <4kof6e$te@news1.mnsinc.com>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4kof6e$te@news1.mnsinc.com> huang@mnsinc.com "Szu-Wen Huang" writes:
  15.  
  16. >Lawrence Kirby (fred@genesis.demon.co.uk) wrote:
  17.  
  18. >: where the code for the latter certainly looks more efficient (although
  19. >: it is difficult to guess the exact instruction timings). I wonder if
  20. >: Watcom compiles the 2nd version correctly.
  21. >
  22. >Why do you say the latter looks more efficient?
  23.  
  24. That's just the way the assembly generated looked. Since it is short here
  25. is the corresponding code for the loops with gcc, all other code is
  26. identical. Judge for yourself:
  27.  
  28. for (i=0; i<16; i++)
  29.     prom[i] = prom[i+i];
  30.  
  31.  
  32.     leal -32(%ebp),%edx
  33.     .align 4
  34. .L15
  35.     movb (%edx),%al
  36.     movb %al,-32(%ebx,%ebp)
  37.     addl $2,%edx
  38.     incl %ebx
  39.     cmpl $15,%ebx
  40.     jbe .L15
  41.  
  42.  
  43.  
  44. for (i=0; i<16; i++)
  45.     prom[i] = prom[2*i];
  46.  
  47.  
  48.     .align 4
  49. .L15
  50.     movb -32(%ebp,%ebx,2),%al
  51.     movb %al,-32(%ebx,%ebp)
  52.     incl %ebx
  53.     cmpl $15,%ebx
  54.     jbe .L15
  55.  
  56. >A naive compiler would
  57. >perform an addition with the former and a multiplication with the latter.
  58.  
  59. I try to avoid naive compilers.
  60.  
  61. >One would almost always expect addition to be faster than multiplication.
  62. >An optimizing compiler might be able to realize they are actually the
  63. >same, and perhaps even decide to implement both as bitwise shifts.
  64.  
  65. I expect most compilers can do this with optimisation turned off.
  66.  
  67. >In any case, I wouldn't expect the former to be slower than the latter.  (It
  68. >certainly *might* be, but chances are slim).
  69.  
  70. -- 
  71. -----------------------------------------
  72. Lawrence Kirby | fred@genesis.demon.co.uk
  73. Wilts, England | 70734.126@compuserve.com
  74. -----------------------------------------
  75.